package cn.edu.xhu.library_management.area.controller;

import cn.edu.xhu.library_management.area.entity.LmArea;
import cn.edu.xhu.library_management.area.entity.dto.LmAreaDTO;
import cn.edu.xhu.library_management.area.entity.search.LmAreaSearch;
import cn.edu.xhu.library_management.area.entity.vo.LmAreaVo;
import cn.edu.xhu.library_management.area.service.AreaService;
import cn.edu.xhu.library_management.common.entity.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 14
 * @Description: 区域控制器
 */
@Api(tags = "区域模块")
@ApiSort(3)
@RestController
@RequestMapping("/lm_area")
public class AreaController {
    @Autowired
    private AreaService areaService;


    @GetMapping("/area")
    @ApiOperation(value = "通过楼层ID查询所有区域信息")
    @ApiOperationSupport(order = 311)
    public R findAreasByFloorId(@Param("FloorId") Integer FloorId){
        List<LmArea> areas = areaService.findAreasByFloorId(FloorId);
        return !areas.isEmpty()?R.ok().data("areas",areas): R.error("数据为空");
    }
    @GetMapping("/area/type")
    @ApiOperation(value = "通过楼层ID和特定类型查询区域信息")
    @ApiOperationSupport(order = 311)
    public R findAreasByType(@Param("FloorId") Integer FloorId,@RequestParam("type") Integer type){
        List<LmArea> areas = areaService.findAreasByType(FloorId, type);
        return !areas.isEmpty()?R.ok().data("areas",areas): R.error("数据为空");
    }
//    findFloorsByType
    @GetMapping("/area/all/{infoId}")
    @ApiOperation(value = "通过图书馆ID查询区域信息")
    @ApiOperationSupport(order = 312)
    public R findAreasByInfoId(@PathVariable("infoId") Integer infoId){
        List<LmAreaVo> areas = areaService.findAreasByInfoId(infoId);
        return !areas.isEmpty()?R.ok().data("areas",areas): R.error("数据为空");
    }
    @GetMapping("/area/{id}")
    @ApiOperation(value = "通过区域ID查询区域信息")
    @ApiOperationSupport(order = 313)
    public R findAreaById(@PathVariable("id") Integer id){
        LmArea area = areaService.findAreaById(id);
        return area != null ? R.ok().data("area",area):R.error("查无此数据!!!");
    }

    @GetMapping("/area/search")
    @ApiOperation(value = "区域搜索")
    @ApiOperationSupport(order = 314)
    public R search(LmAreaSearch search){
        List<LmAreaVo> areas = areaService.search(search);

        return !areas.isEmpty()?R.ok().data("areas",areas): R.error("数据为空");
    }



    @PostMapping("/area")
    @ApiOperation(value = "添加区域信息")
    @ApiOperationSupport(order = 321)
    public R addArea(@RequestBody LmAreaDTO areaDTO){
        return areaService.addArea(areaDTO)?R.ok("添加区域信息成功!!!"):R.error("添加区域信息失败!!!");
    }

    @PutMapping("/area")
    @ApiOperation(value = "修改区域信息")
    @ApiOperationSupport(order = 331)
    public R updateArea(@RequestBody  LmAreaDTO areaDTO){
        return areaService.updateArea(areaDTO)?R.ok("修改区域信息成功!!!"):R.error("修改区域信息失败!!!");
    }

    @DeleteMapping("/area/{id}")
    @ApiOperation(value = "删除区域信息")
    @ApiOperationSupport(order = 341)
    public R deleteAreaById(@PathVariable("id") Integer id){
        return areaService.deleteAreaById(id) ?R.ok("删除区域信息成功!!!"):R.error("删除区域信息失败!!!");
    }
}
